package ar.edu.unlu.sistemas.p2p.android;

import java.util.Collection;
import java.util.Iterator;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import ar.edu.unlu.sistemas.p2p.business.vo.PeerVO;
import ar.edu.unlu.sistemas.p2p.business.vo.PingResultVO;

public class SomeActionActivity extends AbsP2PActivity implements OnClickListener{

	private final static  int PEER_TXT_ID = 8888;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen);
        Button btn = (Button)findViewById(R.id.button_do_some_action);
        btn.setOnClickListener(this);
        RadioButton rbtn = (RadioButton)findViewById(R.id.radio_button_connect2peer);
        rbtn.setOnClickListener(this);
        rbtn = (RadioButton)findViewById(R.id.radio_button_peers);
        rbtn.setOnClickListener(this);
        rbtn = (RadioButton)findViewById(R.id.radio_button_ping);
        rbtn.setOnClickListener(this);
        bindService(new Intent(SomeActionActivity.this,AppService.class), mConnection, BIND_AUTO_CREATE);
    }
    
    public void onServiceConnectedWithActivity() {
    	super.onServiceConnectedWithActivity();
    	if (!appService.isConnected()) {
    		RadioButton rbtn = (RadioButton)findViewById(R.id.radio_button_connect2peer);
            rbtn.setEnabled(false);
            rbtn = (RadioButton)findViewById(R.id.radio_button_peers);
            rbtn.setEnabled(false);
            rbtn = (RadioButton)findViewById(R.id.radio_button_ping);
            rbtn.setEnabled(false);
    	}
    };
    
    public void onClick(View v) {
    	EditText tv;
    	switch (v.getId()) {
		case R.id.button_do_some_action:
			RadioGroup rg = (RadioGroup) this.findViewById(R.id.radio_group_some_action);
			int sel = rg.getCheckedRadioButtonId();
			switch (sel) {
				case R.id.radio_button_ping:
					Runnable runnable =	
						new Runnable() {
							public void run() {
								doingPing();
				        	}
				    	};
				    runnable.run();
					break;
				case R.id.radio_button_peers:
					this.getingPeers();
					break;
				case R.id.radio_button_connect2peer:
					this.connectToPeer();
					break;
				default:
					break;
			}
			break;
		case PEER_TXT_ID:
			this.createPeerDialog((PeerVO)v.getTag());
			break;
		case R.id.radio_button_ping:
			tv = (EditText) this.findViewById(R.id.text_connect_peer_addr);
			tv.setVisibility(View.GONE);
			tv = (EditText) this.findViewById(R.id.text_connect_peer_port);
			tv.setVisibility(View.GONE);
			break;
		case R.id.radio_button_peers:
			tv = (EditText) this.findViewById(R.id.text_connect_peer_addr);
			tv.setVisibility(View.GONE);
			tv = (EditText) this.findViewById(R.id.text_connect_peer_port);
			tv.setVisibility(View.GONE);
			break;
		case R.id.radio_button_connect2peer:
			tv = (EditText) this.findViewById(R.id.text_connect_peer_addr);
			tv.setVisibility(View.VISIBLE);
			tv = (EditText) this.findViewById(R.id.text_connect_peer_port);
			tv.setVisibility(View.VISIBLE);
			break;
    	default:
    		break;
    	}
    }
    
    private void doingPing() {
    	boolean flag = true;
    	Iterator<PingResultVO> it;
    	String msgId = this.appService.ping();
    	LinearLayout ll = (LinearLayout) this.findViewById(R.id.layout_results);
    	ll.removeAllViewsInLayout();
    	while (flag) {
    		ll.removeAllViewsInLayout();
    		if(!this.appService.isTaskFinished(msgId)) {
    			it = this.appService.getPingResults(msgId).iterator();
    		} else {
    			flag = false;
    			it = this.appService.finishPingResults(msgId).iterator();
    		}
    		while (it.hasNext()) {
    			TextView tv = new TextView(this);
    			tv.setText(it.next().toString());
    			ll.addView(tv);
    		}
    	}
    }
    
    private void getingPeers() {
    	Collection<PeerVO> coll = this.appService.getPeers();
		Iterator<PeerVO> it = coll.iterator();
		PeerVO vo;
		LinearLayout ll = (LinearLayout) this.findViewById(R.id.layout_results);
		ll.removeAllViewsInLayout();
		while (it.hasNext()) {
			vo = it.next();
			TextView tv = new TextView(this);
			tv.setId(PEER_TXT_ID);
			tv.setTag(vo);
			//tv.setTextColor(Color.BLACK);
			tv.setTextSize(50);
			tv.setText(vo.getName() != null ? vo.getName() : vo.getId());
			tv.setOnClickListener(this);
			//tv.setBackgroundColor(Color.rgb(176+m, 176+m, 176+m));
			tv.setGravity(Gravity.CENTER_HORIZONTAL);
			ll.addView(tv);
		}
    }
    
    private void connectToPeer() {
    	EditText tv = (EditText) this.findViewById(R.id.text_connect_peer_addr);
		String addr = tv.getText().toString();
		tv.setText(null);
		tv = (EditText) this.findViewById(R.id.text_connect_peer_port);
		int port = Integer.parseInt(tv.getText().toString());
		tv.setText(null);
		this.appService.connectToPeerWithAddress(addr, port);
    }
    
    private void createPeerDialog(PeerVO vo) {
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setCancelable(false)
    	       .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	               dialog.dismiss();
    	           }
    	       });
    	AlertDialog dialog = builder.create();
    	dialog.setTitle("Info del peer");
    	dialog.setMessage(vo.toString());
    	dialog.show();
    }

}
